﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using Includes;

namespace AsyncroneModule
{
   /// <summary>
   /// Used to parse a variety of input values to system internal representations
   /// </summary>
   public static class Parser
   {
      /// <summary>
      /// Parses an SMS and returns Vitals
      /// </summary>
      /// <param name="sms"> SMS struct that contains the string to parse </param>
      /// <returns> Vital struct with the parsed values </returns>
      /// <remarks> Throws couldNotParseException </remarks>
      public static Vitals ParseSMStoVitals(SMS sms)
      {
         return ParseSMStoVitals(sms.text);
      }

      /// <summary>
      /// Parses a String and returns Vitals
      /// </summary>
      /// <param name="text"> String to parse </param>
      /// <returns> Vital struct with the parsed values </returns>
      /// <remarks> Throws couldNotParseException </remarks>
      public static Vitals ParseSMStoVitals(String text)
      {
         Vitals result;

         if(!SyntaxCheck.checkSyntaxVitals(text))
         {
            throw(new CouldNotParseException("Text did not have the correct format"));
         }
         else
         {
            String[] tmp = text.Split(' ');

            try
            {
               result = new Vitals();
               result.blood  = System.Convert.ToInt32(tmp[0],10);
               result.pulse  = System.Convert.ToInt32(tmp[1],10);
               result.weight = System.Convert.ToInt32(tmp[2],10);
            }
            catch(Exception)
            {
               throw(new CouldNotParseException("Converting to attributes failed"));
            }
         }
            
         return result;
      }
      
      /// <summary>
      /// Parses SMS for sender number
      /// </summary>
      /// <param name="sms"> SMS to parse </param>
      /// <returns> Number in system representation </returns>
      public static String parseSMSSenderNumber(SMS sms)
      {
         return parseSMSSenderNumber(sms.senderNumber);
      }

      /// <summary>
      /// Parses a Telephone number and returns the system representation
      /// </summary>
      /// <param name="number"> String to parse </param>
      /// <returns> Number in system notation </returns>
      /// <remarks> Throws couldNotParseException </remarks>
      public static String parseSMSSenderNumber(String number)
      {
         // Decide if sms number can be used in the system
         // Possible numbers are:
         // National:           0rrrnn...nn (example    0 78136991)
         // Global  : (+ or 00)ccrrrnn...nn (example  +49 78136991)
         //                                 (example 0049 78136991)
         // In-system numbers are in "+ccrrrnn...nn" representation
         String newNumber = "+";
         String oldNumber = number;

         try
         {
            // Parse country code
            if (oldNumber.Substring(0, 1).Equals("+"))
               oldNumber = oldNumber.Substring(1);
            else if (oldNumber.Substring(0, 2).Equals("00"))
               oldNumber = oldNumber.Substring(2);
            else if (oldNumber.Substring(0, 1).Equals("0"))
               oldNumber = "49" + oldNumber.Substring(1);
            else
               oldNumber = "NATN"; // Not a telephone number

            newNumber += oldNumber;
         }
         catch (Exception)
         {
            throw (new CouldNotParseException("Number can not be parsed"));
         }

         if (!SyntaxCheck.CheckSyntaxNumber(newNumber))
         {
            throw(new CouldNotParseException("Number was not sucessfully converted"));
         }
         
         return newNumber;
      }
   }
}
